<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">系统管理</a></el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/roles' }">角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片-->
    <el-card class="box-card">

      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="角色名" label-width="70px">
          <el-input v-model="formInline.roleName" placeholder="请输入角色名"></el-input>
        </el-form-item>
        <el-form-item label="备注信息" label-width="70px">
          <el-input v-model="formInline.remark" placeholder="请输入备注信息"></el-input>
        </el-form-item>

        <el-form-item style="margin-left: 10px">
          <el-button icon="el-icon-refresh" v-on:click="clearRole">重置</el-button>
          <el-button type="primary" icon="el-icon-search" v-on:click="queryRoleName"><a >查询</a></el-button>
          <el-button type="success" icon="el-icon-plus" v-on:click="dialogVisible=true">添加</el-button>
          <el-button type="warning" icon="el-icon-download" ><a href="http://localhost:8090/system/role/exportRoleToExcel">导出</a></el-button>
        </el-form-item>
        <el-form-item prop="file">
          <el-upload
            class="upload-demo"
            ref="upload"
            multiple
            accept=".xls"
            action=""
            :http-request="httpRequest"
            :file-list="fileList"
            :auto-upload="false"
          >
            <el-button slot="trigger"  type="primary">选取文件</el-button>
            <el-button style="margin-left:5px" type="danger" icon="el-icon-upload"  @click="submitUploadList">导入</el-button>
          </el-upload>
        </el-form-item>
      </el-form>


      <!--表格内容显示区域-->
      <el-table
        :data="roleList"
        border
        max-height="400px"
        style="width: 100%;height: 400px;">
        <el-table-column
          prop="id"
          label="编号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名"
          width="110">
        </el-table-column>


        <el-table-column
          prop="remark"
          label="备注"
          width="190">
        </el-table-column>
        <el-table-column
          sortable
          prop="createTime"
          label="创建时间"
          width="190">
        </el-table-column>
        <el-table-column
          sortable
          prop="modifiedTime"
          label="最后修改时间"
          width="190">
        </el-table-column>

        <el-table-column
          prop="status"
          label="是否禁用"
          width="100">


          <template slot-scope="scope" >
            <el-switch
              v-on:change="changeStatus(scope.row.status,scope.row.id)"
              v-model="scope.row.status===1"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope" >
          <el-button type="primary" size="mini" icon="el-icon-edit" v-on:click="queryRoleById(scope.row)">修改</el-button>
           <el-button type="danger" size="mini" icon="el-icon-delete" v-on:click="deleteRole(scope.row.id)"> 删除</el-button>
          <el-button type="warning" size="mini" icon="el-icon-s-tools">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="padding-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--添加弹窗-->
    <el-dialog title="添加" :visible.sync="dialogVisible" width="30%">
    <el-form ref="form" :model="roleForm" label-width="80px">
      <el-form-item label="角色名称:">
        <el-input v-model="roleForm.roleName"></el-input>
      </el-form-item>
      <el-form-item label="备注:">
        <el-input v-model="roleForm.remark"></el-input>
      </el-form-item>
      <el-form-item label="创建时间:">
        <div>
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="roleForm.createTime"
            type="datetime"
            style="width:100%;"
            placeholder="选择日期时间"
            align="right"
            :picker-options="pickerOptions1">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="修改时间:">
        <div>
          <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="roleForm.modifiedTime"
            type="datetime"
            style="width:100%;"
            placeholder="选择日期时间"
            align="right"
            :picker-options="pickerOptions1">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="状态:">
        <el-input v-model="roleForm.status" placeholder="是否可用? 0:不可用，1：可用"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
  </el-dialog>
<!--修改弹窗-->
    <el-dialog title="修改" :visible.sync="dialogRole" width="30%">
      <el-form ref="form" :model="roleUpdateList" label-width="80px">
        <el-form-item label="角色名称:">
          <el-input v-model="roleUpdateList.roleName"></el-input>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="roleUpdateList.remark"></el-input>
        </el-form-item>
        <el-form-item label="创建时间:">
          <div>
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="roleUpdateList.createTime"
              type="datetime"
              style="width:100%;"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions1">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="修改时间:">
          <div>
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="roleUpdateList.modifiedTime"
              type="datetime"
              style="width:100%;"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions1">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="状态:">
          <el-input v-model="roleUpdateList.status" placeholder="是否可用? 0:不可用，1：可用"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogRole = false">取 消</el-button>
        <el-button type="primary" @click="updateRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { queryAllRoleByPage,updateRoleById,deleteRoleById } from '../../api/roles';

  export default {
    name: 'Role',
    data () {
      return {
        pageSize: 6,
        total: 1,
        current: 1,
        currentPage4: 4,
        formInline: {
          remark: '',
          roleName: '',
        },
        departments: [],
        roleList: null,
        fileList: [],
        loadUrl:'system/role/importRoleToExcel',
        dialogVisible:false,
        dialogRole:false,
        roleForm: {},
        roleUpdateList: {},
        pickerOptions1: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      }
    },
    methods: {
      // 提交事件
      onSubmit () {
        console.log('submit!')
      },
      //导入数据
      httpRequest(param) {
        console.log(param);
        let fileObj = param.file; // 相当于input里取得的files
        let fd = new FormData(); // FormData 对象
        fd.append("file", fileObj); // 文件对象

        let url = this.loadUrl
        let config = {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        };
        this.$http.post(url, fd, config).then((res) => {
          console.log(res);
        });
      },
      submitUploadList() {
        this.$refs.upload.submit();
      },
      // 当记录数发生改变时
      handleSizeChange (val) {
        this.pageSize = val;
        let roleName=this.formInline.roleName;
        let remark=this.formInline.remark;
        console.log(remark)
        this.getRoleList(this.current,this.size,roleName,remark);
      },
      // 当前页发生改变时
      handleCurrentChange (val) {
        let roleName=this.formInline.roleName;
        let remark=this.formInline.remark;
        this.current = val;
        this.getRoleList(this.current,this.pageSize,roleName,remark);
      },
      // 获取角色数据
      async getRoleList() {
        let roleName=this.formInline.roleName;
        let remark=this.formInline.remark;
        const { data } = await queryAllRoleByPage(this.current,this.pageSize,roleName,remark);
        this.roleList = data.data.rolePage.records;
        this.total = data.data.rolePage.total;
        this.pageSize = data.data.rolePage.size;
      } ,
      //删除角色信息
      async deleteRole(id){
        let roleName=this.formInline.roleName;
        let remark=this.formInline.remark;
        await deleteRoleById(id);
        this.$message({
          type: "info",
          message: "删除成功",
        });
        this.getRoleList(this.current,this.pageSize,roleName,remark);
      },
      //修改状态
      async changeStatus(status,id){
        let roleName=this.formInline.roleName;
        let remark=this.formInline.remark;
        if(status===1){
          status=0;
          await updateRoleById(status,id);
          this.$message({
            type: "info",
            message: "禁用成功",
          });
          this.getRoleList(this.current,this.pageSize,roleName,remark);
          return ;
        }else{
          status=1;
          await updateRoleById(status,id);
          this.$message({
            type: "success",
            message: "启用成功",
          });
          this.getRoleList(this.current,this.pageSize,roleName,remark);
          return ;
        }
      },
      //添加角色信息
      async addRole(){
        let roleName=this.formInline.roleName;
        let remark=this.formInline.remark;
        let url='system/role/insertRole'
        this.$http.post(url, this.roleForm).then((res) => {
          this.getRoleList(this.current,this.pageSize,roleName,remark);
          console.log(res);
          this.roleForm={},
            this.$message({
              type: "info",
              message: "保存成功",
            });
          this.dialogVisible = false;
        }).catch(ex=>
          this.$message({
            type: "error",
            message: "保存失败",
          }));
      },
      //模糊查询角色信息
  async queryRoleName(){
        let roleName=this.formInline.roleName;
        console.log(roleName)
        let remark=this.formInline.remark;
        const { data } =  await queryAllRoleByPage(1,this.pageSize,roleName,remark);
        this.roleList = data.data.rolePage.records;
        this.total = data.data.rolePage.total;
        this.pageSize = data.data.rolePage.size;
      },
      //清空查询条件
      async clearRole(){
        let roleName=this.formInline.roleName='';
        let remark=this.formInline.remark='';
        this.getRoleList(this.current,this.pageSize,roleName,remark);
      },
      //修改角色
      async updateRole(){
        let roleName=this.formInline.roleName;
        let remark=this.formInline.remark;
        let url='system/role/updateRoleById'
        this.$http.post(url, this.roleUpdateList).then((res) => {
          this.getRoleList(this.current,this.pageSize,roleName,remark);
          console.log(res);
          this.$message({
            type: "info",
            message: "保存成功",
          });
          this.dialogRole = false;
        }).catch(ex=> this.$message({
          type: "error",
          message: "保存失败",
        }));
      },
      //获取当前选中用户信息
      async queryRoleById(row){
        this.roleUpdateList=row;
        this.dialogRole=true;
      },
    },
    // create创建完成后钩子函数
    created () {
      this.getRoleList();
    }
  }
</script>

<style scoped>
  a {
    text-decoration: none;
    color:white;
  }
  /*正常的未被访问过的链接*/
  a:link {
    text-decoration: none;
  }
  /*已经访问过的链接*/
  a:visited {
    text-decoration: none;
  }
  /*鼠标划过(停留)的链接*/
  a:hover {
    text-decoration: none;
  }
  /* 正在点击的链接，鼠标在元素上按下还没有松开*/
  a:active {
    text-decoration: none;
  }
  /* 获得焦点的时候 鼠标松开时显示的颜色*/
  a:focus {
    text-decoration: none;
  }
  .upload-demo input{
    display: none;
  }
</style>
